﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZJ_HZJEInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            Button btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            Label 征收批次号 = (Label)FindControl("征收批次号");

            TextBox txt划转金额 = (TextBox)FindControl("txt划转金额");
            MPCalendar txt划转时间 = (MPCalendar)FindControl("txt划转时间");
            TextBox txt备注 = (TextBox)FindControl("txt备注");

            TextBox txtBID = (TextBox)FindControl("txtBID");
            TextBox txtURID = (TextBox)FindControl("txtURID");
            TextBox txtID = (TextBox)FindControl("txtID");
            TextBox txtPCID = (TextBox)FindControl("txtPCID");
            TextBox txtZSPID = (TextBox)FindControl("txtZSPID");

            string strbid = base.Page.Request["BID"];
            string strpnid = base.Page.Request["PNID"];
            string strpcid = base.Page.Request["PCID"];
            string strpid = base.Page.Request["PID"];
            string strid = base.Page.Request["ID"];

            if (!base.Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(strpcid)) { 
                    txtZSPID.Text = _EluZSDataProvider.GetSomeStr("zs_zspch", "zsuserid", "pcid", strpcid);
                    征收批次号.Text = _EluZSDataProvider.GetSomeStr("zs_zspch", "征收批次号", "pcid", strpcid);
                }
                if (!string.IsNullOrEmpty(strid))
                {
                    if (string.IsNullOrEmpty(txtID.Text.Trim())) { txtID.Text = strid; }
                    EluZSProvider.CHANGE_MONEY changemoney = _EluZSDataProvider.GetCHANGEMONEY(strid);

                    txt划转金额.Text = changemoney.划转金额;                   
                    txt备注.Text = changemoney.备注;
                    txtBID.Text = changemoney.BID;
                    txtZSPID.Text = changemoney.ZSPID;
                    txtURID.Text = changemoney.URID;
                    txtPCID.Text = changemoney.PCID;
                    if (changemoney.划转时间.HasValue) { txt划转时间.Text = changemoney.划转时间.Value.ToShortDateString(); }
                }
                else
                {
                    txtID.Text = System.Guid.NewGuid().ToString().Replace("-", "");
                    txt划转金额.Text = _EluZSDataProvider.GetString("select sum(to_number(nvl(replace(划转金额, '\', ''), 0))) from zs_zspch where pcid='" + strpcid + "'");
                    txtURID.Text = userid;
                    txtPCID.Text = strpcid;
                    txtBID.Text = strbid;
                }
                if (_EluZSDataProvider.GetHZJERY(strpcid, "").Rows.Count > 0) { } else {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "displayhzry", "<script>displayhzry();</script>");
                }
            }
            IsLookPerson();
        }
        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btn_Insert = (Button)FindControl("btn_Insert");
                        btn_Insert.Visible = false;
                    }
                }
            }
        }


        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            string strbid = base.Page.Request["BID"];
            string strpnid = base.Page.Request["PNID"];
            string strpcid = base.Page.Request["PCID"];
            string strpid = base.Page.Request["PID"];
            string strid = base.Page.Request["ID"];
            TextBox txt划转金额 = (TextBox)FindControl("txt划转金额");
            MPCalendar txt划转时间 = (MPCalendar)FindControl("txt划转时间");
            TextBox txt备注 = (TextBox)FindControl("txt备注");

            TextBox txtBID = (TextBox)FindControl("txtBID");
            TextBox txtURID = (TextBox)FindControl("txtURID");
            TextBox txtID = (TextBox)FindControl("txtID");
            TextBox txtPCID = (TextBox)FindControl("txtPCID");
            TextBox txtZSPID = (TextBox)FindControl("txtZSPID");

            EluZSProvider.CHANGE_MONEY changemoney = new EluZSProvider.CHANGE_MONEY();

            changemoney.划转金额 = txt划转金额.Text;
            changemoney.备注 = txt备注.Text;
            changemoney.BID = txtBID.Text;
            changemoney.ZSPID = txtZSPID.Text;
            changemoney.URID = txtURID.Text;
            changemoney.PCID = txtPCID.Text;
            changemoney.ID = txtID.Text;
            changemoney.划转时间 = (string.IsNullOrEmpty(txt划转时间.Text) ? null : (DateTime?)Convert.ToDateTime(txt划转时间.Text));

            bool save = _EluZSDataProvider.SaveChangemoney(strid, changemoney);
            if (save) { base.Page.Response.Write("<script>alert('保存成功！');window.location.href('ZJ_HZJEInfo.aspx?ID=" + txtID.Text + "&PID=" + strpid + "&BID=" + strbid + "&PCID=" + strpcid + "&PNID=" + strpnid + "');</script>"); }
        }
    }
}


